In this video you will learn how to create expanding windows so you will see in a minute what I mean
here.
And first of all we import pandas and we are working with the S&P 500 data set and we are only interested
in the last 10 years.
So this is actually nothing new here.
And in the last videos we created rolling windows with a constant window size.
So for example 10 days and by moving forward one day or one timestamp we actually drop the oldest timestamp
and at the most recent time stamp.
So for example we could calculate the rolling mean or the simple moving average for our clothes column
here and our fixed size window is actually here ten days.
So this is actually the rolling average.
And actually in contrast to that with expanding windows our windows increase as we move forward in time.
So at the very first time stem we have only one time for our calculations.
Then at the second timestamp we have two and so on.
And coding wise pandas actually provides us with the surprise expanding method so we can you see the
expanding method on our clothes column and we can calculate actually the expanding mean.
So let's say this here so here we are for the very first time stamp at the thirty first of December
2000 and eight.
And here we have the value of nine hundred three point two five.
And uh.
Yeah.
This is uh simply the very first time stamp here in our close column and then at the second time stamp.
So it's uh the second of January 2009.
If you calculated the expanding mean then pandas simply takes uh the average of those two time stamps
and then at the fifth of January 2009 pandas calculate the mean of um the.
Immediately three proceeding uh timestamps here and so on so let's go down here now and that's actually
for the expanding method.
Uh one parameter.
So let's have a look.
So here we have the min period parameter.
And if we can define the minimum number of observations in a window required to have a value.
So the default setting is 1 but we can also set this for example to 5
so we can see that we get the very first value at the fifth time stamp.
So here we are in total five.
Immediately preceding timestamps actually.
But let's keep here the min periods per meter 2 1
and 2 comparison Bill moving averages and expanding windows.
We can create the two new columns.
So the a simple moving average is 50.
And uh the expanding window.
So let's do this here and let's have a look.
So uh here follow a simple moving averages.
The first the 50 values are obviously any and values.
So no surprise here.
And now we can also visualize those two columns by using the other plot method.
So here we can see in blue the simple moving average 50 and in green the expanding window.
So at each single point in time here we have uh the average price for all of the preceding timestamps.
And we can see here this is a quite stable line.
And the same method with the rolling method you can do more than just the calculating expanding averages.
So for example we can also calculate the expanding minimum stock price for example by changing here
the main method so let's have a look
so this this year the expanding minimum and obviously the total minimum here in the period between 2009
and 2018 is here directly in the year 2009.
And we could also visualize the expanding maximum with the max method
so here we have in green the expanding maximum.
And as our S&P 500 index this year increasing from 2009 to 2018 our expanding maximum see are also increasing.
So these were fewer examples and applications of expanding windows.
And there's actually a lot of more to discover here but for the time being we are finishing with this
video and I hope to see you also in the next one by.